{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "918fa452",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9e0748d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "API_key = 'KErJq5Agb1znnsUemM6Wl3rfzD3yZaos'\n",
    "API_sercret = 'KXwjBv0j2tkGRYPul09Z8a_CwtYdqEGM'\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    \n",
    "}\n",
    "\n",
    "r = requests.post(base_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "51849639",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d8ada163",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647344491,fe60352c-8c3e-451c-8b7a-645d3bc182cb',\n",
       " 'time_used': 630,\n",
       " 'faces': [{'face_token': '5b3d52441e67e416ecd5a05fb852ed80',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "547cd2b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detect = r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "342569cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_tokens = face_detect['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "79f04b8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'5b3d52441e67e416ecd5a05fb852ed80'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02d15df1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d38b0494",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detect_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "API_key = 'KErJq5Agb1znnsUemM6Wl3rfzD3yZaos'\n",
    "API_sercret = 'KXwjBv0j2tkGRYPul09Z8a_CwtYdqEGM'\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':face_tokens,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "    \n",
    "}\n",
    "\n",
    "r_analyze = requests.post(url = face_detect_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1aaee48d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0c8be058",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 191,\n",
       " 'request_id': '1647345703,ab9c23f9-1f93-465e-bb14-427997c66257',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.039,\n",
       "     'neutral': 0.001,\n",
       "     'disgust': 0.191,\n",
       "     'anger': 0.001,\n",
       "     'surprise': 0.031,\n",
       "     'fear': 0.001,\n",
       "     'happiness': 99.736},\n",
       "    'beauty': {'female_score': 58.555, 'male_score': 58.212},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 53},\n",
       "    'mouthstatus': {'close': 0.0,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 100.0,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 1.554,\n",
       "     'stain': 9.17,\n",
       "     'acne': 1.527,\n",
       "     'health': 11.352},\n",
       "    'headpose': {'yaw_angle': 2.7483263,\n",
       "     'pitch_angle': 14.900063,\n",
       "     'roll_angle': 0.5866398},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.376},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.376},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.376}},\n",
       "    'smile': {'threshold': 50.0, 'value': 100.0},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.005,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.995,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.003,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.001,\n",
       "      'no_glass_eye_open': 99.996,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 85.357},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.494,\n",
       "      'vector_z_component': 0.935,\n",
       "      'vector_x_component': -0.068,\n",
       "      'vector_y_component': 0.348,\n",
       "      'position_y_coordinate': 0.438},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.502,\n",
       "      'vector_z_component': 0.942,\n",
       "      'vector_x_component': 0.247,\n",
       "      'vector_y_component': 0.227,\n",
       "      'position_y_coordinate': 0.422}}},\n",
       "   'face_rectangle': {'width': 107, 'top': 108, 'left': 141, 'height': 107},\n",
       "   'face_token': '5b3d52441e67e416ecd5a05fb852ed80'}]}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c5822f3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 191,\n",
       " 'request_id': '1647345703,ab9c23f9-1f93-465e-bb14-427997c66257',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.039,\n",
       "     'neutral': 0.001,\n",
       "     'disgust': 0.191,\n",
       "     'anger': 0.001,\n",
       "     'surprise': 0.031,\n",
       "     'fear': 0.001,\n",
       "     'happiness': 99.736},\n",
       "    'beauty': {'female_score': 58.555, 'male_score': 58.212},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 53},\n",
       "    'mouthstatus': {'close': 0.0,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 100.0,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 1.554,\n",
       "     'stain': 9.17,\n",
       "     'acne': 1.527,\n",
       "     'health': 11.352},\n",
       "    'headpose': {'yaw_angle': 2.7483263,\n",
       "     'pitch_angle': 14.900063,\n",
       "     'roll_angle': 0.5866398},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.376},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.376},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.376}},\n",
       "    'smile': {'threshold': 50.0, 'value': 100.0},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.005,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.995,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.003,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.001,\n",
       "      'no_glass_eye_open': 99.996,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 85.357},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.494,\n",
       "      'vector_z_component': 0.935,\n",
       "      'vector_x_component': -0.068,\n",
       "      'vector_y_component': 0.348,\n",
       "      'position_y_coordinate': 0.438},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.502,\n",
       "      'vector_z_component': 0.942,\n",
       "      'vector_x_component': 0.247,\n",
       "      'vector_y_component': 0.227,\n",
       "      'position_y_coordinate': 0.422}}},\n",
       "   'face_rectangle': {'width': 107, 'top': 108, 'left': 141, 'height': 107},\n",
       "   'face_token': '5b3d52441e67e416ecd5a05fb852ed80'}]}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "5680fbb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "API_key = 'KErJq5Agb1znnsUemM6Wl3rfzD3yZaos'\n",
    "API_sercret = 'KXwjBv0j2tkGRYPul09Z8a_CwtYdqEGM'\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi8.hexun.com%2F2021-07-19%2F203983709.png&refer=http%3A%2F%2Fi8.hexun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649939503&t=256922e4f6194be9e77ad32db391af84'    \n",
    "}\n",
    "\n",
    "r = requests.post(base_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "24b200ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "0b42f6ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647347533,0be5e3a8-4ec5-4059-8c4d-98a8f14690e5',\n",
       " 'time_used': 457,\n",
       " 'faces': [{'face_token': '76242b956f9d32779800b12bb2ce93e5',\n",
       "   'face_rectangle': {'top': 206, 'left': 487, 'width': 189, 'height': 189}}],\n",
       " 'image_id': 'oaSm8m1t78U72ybZ/UizVQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "c3901a63",
   "metadata": {},
   "outputs": [],
   "source": [
    "liu_face1 = '5b3d52441e67e416ecd5a05fb852ed80'\n",
    "liu_face2 = '76242b956f9d32779800b12bb2ce93e5'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "d67d51fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_analyze_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "API_key = 'KErJq5Agb1znnsUemM6Wl3rfzD3yZaos'\n",
    "API_sercret = 'KXwjBv0j2tkGRYPul09Z8a_CwtYdqEGM'\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_token1':'liu_face1',\n",
    "    'face_token2':'liu_face2',\n",
    "}\n",
    "\n",
    "r = requests.post(face_analyze_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "1512a217",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "96b3a757",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647347870,092683b4-9988-4bba-ab9e-69722c8003c5',\n",
       " 'time_used': 61,\n",
       " 'error_message': \"INVALID_FACE_TOKEN: ['liu_face1']\"}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4022a781",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
